var p=true, t = true,n = true, c=false, im =false;

function submitUpdates(formId){
    var up = confirm("Confirm Update Values?");
    if (up == true || up == 1)
    {
        if(formId.id === "createstock"){
            alert("p :"+ p +"t :" + t + "n :" + n +"c :"+c+"im : "+im);
            if(checkAll()){
                formId.submit();
            }else{
                alert('tete');
                alert('Please Enter All Fields');
                return false;
            }
        }else{
             if(checkEditStock()){
                formId.submit();
            }else{
                alert('Please Enter All Fields');
                return false;
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}

function checkAll(){
    if(p && t && n && c &&im){
        document.getElementById("verified").style.display ='block';
        return true;
    }else{
        document.getElementById("verified").style.display ='none';
        return false;
    }
}
function checkEditStock(){
    if(p && t && n){
        document.getElementById("verified").style.display ='block';
        return true;
    }else{
        document.getElementById("verified").style.display ='none';
        return false;
    }
}

function nameTimer(ele){

    var typingTimer = 0;
    var doneTypingInterval = 2000;
    clearTimeout(typingTimer);
    if (ele.value) {
        typingTimer = setTimeout(validateName(ele), doneTypingInterval);
    }
  
}

function priceTimer(ele){

    var typingTimer = 0;
    var doneTypingInterval = 2000;
    clearTimeout(typingTimer);
    if (ele.value) {
        typingTimer = setTimeout(validatePrice(ele), doneTypingInterval);
    }
}
function discountTimer(ele){

    var typingTimer = 0;
    var doneTypingInterval = 2000;
    clearTimeout(typingTimer);
    if (ele.value) {
        typingTimer = setTimeout(validateDiscount(ele), doneTypingInterval);
    }
}

function intTimer(ele){

    var typingTimer = 0;
    var doneTypingInterval = 2000;
    clearTimeout(typingTimer);
    if (ele.value) {
        typingTimer = setTimeout(validateInt(ele), doneTypingInterval);
    }
}
function isNumber(n) {


    return !isNaN(parseFloat(n)) && isFinite(n);
}
   

function checkDec(price){

    var decimal = price.indexOf(".");
    if(decimal < 0){
        return true;
    }else if (decimal+3 < price.length){
        return false;
    }else{
        return true;
    }
}


function validatePrice(ele){
    var price = ele.value.split(' ').join('');
    var container= document.getElementById("error"+ele.id);
    p = false;
    if(price === "0" || price ==='' || price === null ){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\"> Sorry, Please Give Me A Price </font>";
    }else if(!isNumber(price)){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">I Need Numbers Not Characters! </font>";
    }else if(!checkDec(price)){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\"> Up To 2 Decimal Place Only! </font>";
    }else{
        ele.setAttribute("class", "");
        container.innerHTML = "<font color=\"Green\"> Good! </font>";
        p = true;
    }

    ele.value = price;
    checkAll();
}


function validateDiscount(ele){
    var price = ele.value.split(' ').join('');
    var container= document.getElementById("error"+ele.id);
    p = false;
    if(price =='' || price == null ){
        ele.innerHTML=0;
    }else if(!isNumber(price)){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">I Need Numbers Not Characters! </font>";
    }else if(!checkDec(price)){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">Up To 2 Decimal Place Only!</font>";
    }else if(price > 100 || price < 0){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\"> Discount Range Only 0 - 100% </font>";
    }else{
        ele.setAttribute("class", "");
        container.innerHTML = "<font color=\"Green\">Good!</font>";
        p = true;
    }

    ele.value = price;
    checkAll();
}


function validateInt(ele){
    var container = document.getElementById("error"+ele.id);
    var threshold = ele.value.split(' ').join('');
    t=false;
    if(threshold =='' || threshold == null ){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">Sorry, Only Numbers Please </font>";
    }else if(!isNumber(threshold)){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">I Need Numbers Not Characters! </font>";
    }else{
        ele.setAttribute("class", "");
        container.innerHTML = "<font color=\"Green\">Good! </font>";
        t= true;
    }
    checkAll();
   
}

function validateName(ele){
    var container = document.getElementById("erroritemname");
    var name = ele.value;
    container.style.display= 'block';
    n= false;
    if(name == '' || name == null ){
        ele.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">Hey!.. I need a Name!</font>";
    }else{
        ele.setAttribute("class", "");
        container.innerHTML = "<font color=\"Green\">Good!</font>";
        n= true;
    }
    checkAll();
}

var hash = {
    '.jpg'  : 1,
    '.jpeg'  : 1,
    '.gif'  : 1,
    '.JPG'  : 1,
    '.JPEG'  : 1,
    '.GIF'  : 1,
    '.PNG'  : 1,
    '.png' : 1,
    '.bmp' : 1,
    '.BMP' : 1
};

function check_extension(filename,upload) {

    var re = /\..+$/;
    var ext = (filename.value).match(re);
    var container = document.getElementById("errorimage");
    if (hash[ext]) {
        container.style.display = 'hidden';
        filename.setAttribute("class", "");
        im = true;
        uploadFile();
        document.forms[upload].submit();
        return true;
    } else {
        container.style.display= 'block';
        filename.setAttribute("class", "requiredField");
        container.innerHTML = "<font color=\"red\">Invalid file please select an image file</font>";
        filename.value= "";
        im=false;
        return false;
    }
}



function CountCheck(l){
    
    var p;
     var i = document.getElementById('cbox').value;
     if(document.getElementById('checkstore'+l).checked){
        i++;
     }else{
         i--;
     }
      document.getElementById('cbox').value = i;
     if(i>0){
        c= true;
     }else{
         c= false;
     }
    

}